package com.yang.controller;

import java.io.IOException;
import java.util.Map;

import javax.jws.WebMethod;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.sword.wechat4j.common.Config;
import org.sword.wechat4j.param.WechatParamName;
import org.sword.wechat4j.token.Sign;
import org.sword.wechat4j.token.TokenProxy;

/**
 * Servlet implementation class IndexAction
 */
@WebServlet("/index")
public class IndexAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IndexAction() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String jsapi_ticket = TokenProxy.jsApiTicket();
		String url = "http://yang.ngrok.cc/weixin/index";
		Map<String, String> ret = Sign.sign(jsapi_ticket, url);
		request.setAttribute("signature", ret.get("signature"));
		request.setAttribute("nonceStr", ret.get("nonceStr"));
		request.setAttribute("timestamp", ret.get("timestamp"));
		request.setAttribute("appId",Config.instance().getAppid());
		request.getRequestDispatcher("/page/index.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
